package cn.gov.msa.ws;


import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;

import cn.gov.msa.model.SuperInspecRecord;
import net.sf.json.JSONObject;

public class InspectService {
	
	private static Logger logger = Logger.getLogger(InspectService.class);
	
	public static String hydInspect(ApplicationContext ctx,String params) throws Exception {
		cn.gov.msa.service.SuperInspecRecordService inspectService = (cn.gov.msa.service.SuperInspecRecordService) ctx.getBean("SuperInspecRecordService");
		JSONObject inspectJson = JSONObject.fromObject(params);
		String result = "{\"result\":\"1\",\"resultinfo\":\"接口服务执行成功！\"}";
		if(inspectJson.has("hydInspect")){
			JSONObject inspectMain = inspectJson.getJSONObject("hydInspect");
			if(!"".equals(inspectMain.optString("work_no"))){
				SuperInspecRecord record = new SuperInspecRecord();
				record.setApplyid(inspectMain.getString("work_no"));
				record.setInspectreplycontent(params);
				record.setInspectreplydate(new Date());
				int count = inspectService.updateByUniqueId(record);
				if (0 == count) {
					logger.error("系统未找到与此检查结果对应的数据. data:" + params);
					result = "{\"result\":\"0\",\"resultinfo\":\"未找到与此检查结果对应的数据.\"}";
				}
			} else {
				result = "{\"result\":\"0\",\"resultinfo\":\"检查结果数据缺少work_no(系统数据主键)\"}";
				logger.error("水上水下活动检查结果数据缺少work_no(系统数据主键). data:" + params);
			}
		} else {
			logger.error("水上水下活动检查报告.推送数据格式不正确！data:" + params);
			result = "{\"result\":\"0\",\"resultinfo\":\"推送数据格式不正确！\"}";
		}
		return result;
	}
	
	public static String hydInspectDelete(ApplicationContext ctx,String param) throws Exception {
		JSONObject inspectDeleteJson = JSONObject.fromObject(param);
		String result = "{\"result\":\"1\",\"resultinfo\":\"接口服务执行成功！\"}";
		if(inspectDeleteJson.has("result_no")){
			if("".equals(inspectDeleteJson.optString("result_no"))){
				result = "{\"result\":\"0\",\"resultinfo\":\"检查结果数据缺少result_no(检查数据主键)\"}";
				logger.error("水上水下活动检查结果数据缺少result_no(检查数据主键). data:" + param);
			} else {
				logger.info("删除水上水下活动检查结果数据.result_no:" + inspectDeleteJson.getString("result_no"));
			}
		} else {
			logger.error("水上水下活动检查报告.推送数据格式不正确！data:" + param);
			result = "{\"result\":\"0\",\"resultinfo\":\"推送数据格式不正确！\"}";
		}
		return result;
	}
	
	public static String adminInspect(ApplicationContext ctx,String params) throws Exception {
		cn.gov.msa.service.SuperInspecRecordService inspectService = (cn.gov.msa.service.SuperInspecRecordService) ctx.getBean("SuperInspecRecordService");
		JSONObject inspectJson = JSONObject.fromObject(params);
		String result = "{\"result\":\"1\",\"resultinfo\":\"接口服务执行成功！\"}";
		if(inspectJson.has("adminInspect")){
			JSONObject inspectMain = inspectJson.getJSONObject("adminInspect");
			if(!"".equals(inspectMain.optString("work_no"))){
				SuperInspecRecord record = new SuperInspecRecord();
				record.setApplyid(inspectMain.getString("work_no"));
				record.setInspectreplycontent(params);
				record.setInspectreplydate(new Date());
				int count = inspectService.updateByUniqueId(record);
				if (0 == count) {
					logger.error("系统未找到与此检查结果对应的数据. data:" + params);
					result = "{\"result\":\"0\",\"resultinfo\":\"未找到与此检查结果对应的数据.\"}";
				}
			} else {
				result = "{\"result\":\"0\",\"resultinfo\":\"检查结果数据缺少work_no(系统数据主键)\"}";
				logger.error("行政备案检查结果数据缺少work_no(系统数据主键). data:" + params);
			}
		} else {
			logger.error("行政备案检查报告.推送数据格式不正确！");
			result = "{\"result\":\"0\",\"resultinfo\":\"推送数据格式不正确！\"}";
		}
		return result;
	}
	
	public static String adminInspectDelete(ApplicationContext ctx,String param) throws Exception {
		JSONObject inspectDeleteJson = JSONObject.fromObject(param);
		String result = "{\"result\":\"1\",\"resultinfo\":\"接口服务执行成功！\"}";
		if(inspectDeleteJson.has("inspect_no")){
			if("".equals(inspectDeleteJson.optString("inspect_no"))){
				result = "{\"result\":\"0\",\"resultinfo\":\"检查结果数据缺少inspect_no(检查数据主键)\"}";
				logger.error("行政备案检查结果数据缺少inspect_no(系统数据主键). data:" + param);
			} else {
				logger.info("删除行政备案检查结果数据.inspect_no:" + inspectDeleteJson.getString("inspect_no"));
			}
		} else {
			logger.error("行政备案检查报告.推送数据格式不正确！data:" + param);
			result = "{\"result\":\"0\",\"resultinfo\":\"推送数据格式不正确！\"}";
		}
		return result;
	}

}
